package com.ruoyi.mall.product.interfaces.facade;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.valid.AddGroup;
import com.ruoyi.common.core.valid.UpdateGroup;
import com.ruoyi.common.core.valid.UpdateStatusGroup;
import com.ruoyi.mall.product.service.PmsBrandAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mall.product.domain.entity.PmsBrand;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 品牌Controller
 *
 * @author zhangqishu
 * @date 2022-05-25
 */
@RestController
@RequestMapping("/brand")
public class PmsBrandController extends BaseController {
    @Autowired
    private PmsBrandAppService pmsBrandAppService;

    /**
     * 查询品牌列表
     */
    @RequiresPermissions("product:brand:list")
    @GetMapping("/list")
    public TableDataInfo list(PmsBrand pmsBrand) {
        startPage();
        List<PmsBrand> list = pmsBrandAppService.selectPmsBrandList(pmsBrand);
        return getDataTable(list);
    }

    /**
     * 导出品牌列表
     */
    @RequiresPermissions("product:brand:export")
    @Log(title = "品牌", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmsBrand pmsBrand) {
        List<PmsBrand> list = pmsBrandAppService.selectPmsBrandList(pmsBrand);
        ExcelUtil<PmsBrand> util = new ExcelUtil<PmsBrand>(PmsBrand.class);
        util.exportExcel(response, list, "品牌数据");
    }

    /**
     * 获取品牌详细信息
     */
    @RequiresPermissions("product:brand:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(pmsBrandAppService.selectPmsBrandById(id));
    }

    /**
     * 新增品牌
     */
    @RequiresPermissions("product:brand:add")
    @Log(title = "品牌", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated(value = AddGroup.class) @RequestBody PmsBrand pmsBrand /** BindingResult bindingResult */) {
//        if(bindingResult.hasErrors()){
//            Map<String, String> map = new HashMap<>();
//            bindingResult.getFieldErrors().forEach((error)->{
//                String message=error.getDefaultMessage();
//                String field=error.getField();
//                map.put("message",message);
//                map.put("filed",field);
//            });
//            return AjaxResult.error("提交的数据不合法",map);
//
//        }
        return toAjax(pmsBrandAppService.insertPmsBrand(pmsBrand));
    }

    /**
     * 修改品牌  保证和关联表一致
     */
    @RequiresPermissions("product:brand:edit")
    @Log(title = "品牌", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated(value = UpdateGroup.class) @RequestBody PmsBrand pmsBrand) {
        return toAjax(pmsBrandAppService.updatePmsBrand(pmsBrand));
    }

    /**
     * 修改品牌状态
     */
//    @RequiresPermissions("product:brand:edit")
    @Log(title = "品牌", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult editStatus(@Validated(value = UpdateStatusGroup.class) @RequestBody PmsBrand pmsBrand) {
        return toAjax(pmsBrandAppService.updatePmsBrandStatus(pmsBrand));
    }

    /**
     * 删除品牌
     */
    @RequiresPermissions("product:brand:remove")
    @Log(title = "品牌", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pmsBrandAppService.deletePmsBrandByIds(ids));
    }
}
